<?php

/**
 * Framework_Presenter_Smarty
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @subpackage  Presenter
 * @filesource
 */


/**
 * Framework_Presenter_Smarty
 *
 * By default we use Smarty as our websites presentation layer (view). Smarty
 * is a robust compiling template engine with an active community.
 *
 * @author      Joe Stump <joe@joestump.net>
 * @package     Framework
 * @subpackage  Presenter
 * @link        http://smarty.php.net
 */
class Framework_Presenter_SmartyDebug extends Framework_Presenter_Common
{
	/**
	 * $template
	 *
	 * The templating system, such as SMARTY
	 *
	 * @access      protected
	 * @var         object      $template       Instance of Smarty
	 */
	protected $template = null;

	/**
	 * __construct
	 *
	 * @access      public
	 * @param       object      $module         Instance of Framework_Module
	 * @return      void
	 */
	public function __construct(Framework_Module $module)
	{
		parent::__construct($module);
		$this->template = & Framework_Template::factory('Smarty', Framework::$request->module);
	}

	/**
	 * display
	 *
	 * @access public
	 * @return void
	 */
	public function display()
	{
		if ($cacheData = $this->module->templateCache) {
			// TODO: validate cacheData -> make sure it has id, template, and presenter
			
			$this->template->caching = 1;
			$this->template->cache_lifetime = ((int)Framework::$site->config->cache->cacheLife)*2;
			$this->template->compile_check = true;
			if($this->template->is_cached($cacheData['pageTemplate'],$cacheData['id'])) {
				
				$this->template->display($cacheData['pageTemplate'],$cacheData['id']);
				
				return;
			}
		} else if ($this->module->isCacheable && !count($_POST) && !isset($_GET['noCache'])) {
			
			$this->template->caching = 1;
			$this->template->cache_lifetime = ((int)Framework::$site->config->cache->cacheLife)*2;
			$this->template->compile_check = true;
			
			$handler = $this->module->getCacheHandler();
			$cache = Framework_Cache::factory($handler->getCache());
			$cacheData = $handler->getDataToCache();
			$cache->save($cacheData,$handler->getID(),$handler->getGroup());
		}
   
		
		$path = Framework_Template::getPath($this->module->tplFile, Framework::$request->module);
		$this->template->assign('modulePath', $path);
		$this->template->assign('site',Framework::$site);
		$this->template->assign('tplFile',$this->module->tplFile);
		$this->template->assign('user',$this->user);
		$this->template->assign('session',$this->session);
		$this->template->assign('filePath',Framework::$request->getBaseHTTPPath(false));
		$this->template->assign('uriPath',Framework::$request->getBaseHTTPPath());
		$this->template->assign('request',Framework::$request);
	
		
		//$this->template->assign('breadcrumb', $this->module->getBreadcrumb());

		foreach ($this->module->getData() as $var => $val) {
			if (!in_array($var,array('path','tplFile'))) {
				$this->template->assign($var,$val);
			}
		}

		// Check to see if module has form objects
		// if so, render them to array and assign them
		if (count($this->module->forms)){
			require_once "HTML/QuickForm/Renderer/ArraySmarty.php";
			$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->template);
			foreach ($this->module->forms as $var => $val){
				$val->accept($renderer);
				$this->template->assign($var,$renderer->toArray());
			}
		}

		if ($this->module->pageTemplateFile == null) {
			$this->module->pageTemplateFile = 'Page.tpl';
		}

		$pageTemplateFile = $this->module->pageTemplateFile;

		//DEBUG
        $vars = array(array('POST',$_POST),
        			  array('GET',$_GET),
        			  array('COOKIE',$_COOKIE),
        			  array('SESSION',$_SESSION),
        			  array('SERVER',$_SERVER));
        foreach ($vars as $var) {
            echo '<h2>$'.$var[0].'</h2>'."\n";
            echo '<pre>';
            var_dump($var[1]);
            echo '</pre>';
        }
        
        //SMARTY DUMP
        echo '<h2>PRESENTER</h2><pre>';
        print_r($this);
        echo '</pre>';



	}
}

?>
